﻿#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2\imgproc\types_c.h>

using namespace cv;
using namespace std;

int main()
{
    Mat edges, dstImage, ele;
    vector<vector<Point>>contours;
    vector<Vec4i>hierarchy;
    VideoCapture capture("C://Users//Administrator//Desktop//AA.mp4");	//打开视频文件
    if (capture.isOpened())
        cout << "fail to open!" << endl;
    long totalFrameNumber = capture.get(CAP_PROP_FRAME_COUNT);
    cout << "整个视频共" << totalFrameNumber << "帧" << endl;
    long currentFrame = 0;
    while (true)
    {
        Mat frame;
        if (!capture.read(frame))	//读取每一帧
        {
            cout << "读取视频失败" << endl;
            return -1;
        }
        currentFrame++;	//帧计数
        cout << "正在读取第" << currentFrame << "帧" << endl;
        cvtColor(frame, edges, COLOR_BGR2GRAY);	//转化为灰度图

        GaussianBlur(edges, edges, Size(7, 7), 1.5, 1.5);	//把edges里面的图像做高斯模糊
        for (int i = 0; i<edges.rows; i++)	//阈值二值化
            for (int j = 0; j<edges.cols; j++)
                if ((edges.at<unsigned char>(i, j) < 50) && (edges.at<unsigned char>(i, j) > 40))
                    edges.at<unsigned char>(i, j) = 255;
                else edges.at<unsigned char>(i, j) = 0;
        imshow("取视频", edges);
        ele = getStructuringElement(MORPH_RECT, Size(3, 3));	//腐蚀操作
        erode(edges, edges, ele);
        ele = getStructuringElement(MORPH_RECT, Size(40, 60));	//膨胀操作
        dilate(edges, edges, ele);
        imshow("视频", edges);
  /*
        Create dynamic structure and sequence.
            stor = cvCreateMemStorage(0);
            cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint), stor);

            // 找到所有轮廓
            cvFindContours(dst, stor, &cont, sizeof(CvContour),
                CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
            // 直接使用CONTOUR中的矩形来画轮廓
            for (; cont; cont = cont->h_next)
            {
                flag = 0;
                CvRect r = ((CvContour*)cont)->rect;
                if (r.height * r.width > CONTOUR_MAX_AERA)
                {
                    cvRectangle(img, cvPoint(r.x, r.y),
                        cvPoint(r.x + r.width, r.y + r.height),
                        CV_RGB(255, 0, 0), 1, CV_AA, 0);
                    flag = 1;
                }
                else
                {
                    flag = 0;
                }
            }
            cvReleaseMemStorage(&stor);
            cvReleaseImage(&pyr);

 */



        findContours(edges, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);	//检测出轮廓
        for (int index = 0; index >= 0; index = hierarchy[index][0])
        {

            //drawContours(edges, contours, index, Scalar(255, 255, 255), FILLED, 8, hierarchy);
            Rect rect = boundingRect(contours[index]);	//找出轮廓最小外界矩形
            //cout << "矩形框" << rect.width << endl;
            if((rect.width > 180) && (rect.width < 400) && (rect.height < 600) && (rect.height > 400))	//限定矩形框的大小
                rectangle(frame, rect, Scalar(0, 0, 255), 3);	//在原图像上画出矩形

            imshow("读取视频", frame);	//显示每一帧
        }
        waitKey(30);
    }
    capture.release();
    return 0;

}
